<template>
  <div style="padding: 15px;">
    <vxe-toolbar id="toolbarId" :setting="{storage: true}" export :refresh="{query: findList}">
      <template v-slot:buttons>
        <el-button @click="findList">查询</el-button>
        <el-button @click="funcAdd">新增</el-button>
      </template>
    </vxe-toolbar>
    <vxe-table
      ref="xTable"
      border
      show-footer
      highlight-hover-row
      row-id="id"
      class="mytable-footer"
      align="center"
      height="700"
      :data="tableData"
      :export-config='{type:"csv"}'
      remote-sort
      @sort-change="sortList"
      :seq-config="{startIndex: listQuery.pageNum * listQuery.pageSize - listQuery.pageSize}"
    >
      <vxe-table-column type="seq" width="60"></vxe-table-column>
<!--      <vxe-table-column type="checkbox" width="60"></vxe-table-column>-->
      <vxe-table-column>
        <vxe-table-column field="statusText" title="审核状态" min-width="170" sortable/>
        <template v-slot:header="{ column }">
          <el-select v-model="listQuery.status" placeholder="请选择审核状态" clearable>
            <el-option value="0" label="未审核"/>
            <el-option value="1" label="审核通过"/>
            <el-option value="2" label="审核未通过"/>
          </el-select>
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column field="supplierCode" title="供应商编码" min-width="150" sortable/>
        <template v-slot:header="{ column }">
          <el-input v-model="listQuery.supplierCode" placeholder="请输入供应商编码" clearable/>
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column field="name" title="供应商名称" min-width="150" sortable/>
        <template v-slot:header="{ column }">
          <el-input v-model="listQuery.name" placeholder="请输入供应商名称" clearable/>
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column field="contacts" title="联系人" min-width="150" sortable/>
        <template v-slot:header="{ column }">
          <el-input v-model="listQuery.contacts" placeholder="请输入联系人" clearable/>
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column field="phone" title="手机号" min-width="150" sortable/>
        <template v-slot:header="{ column }">
          <el-input v-model="listQuery.phone" placeholder="请输入手机号" clearable/>
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column field="createdName" title="创建人" min-width="150" sortable/>
        <template v-slot:header="{ column }">
          <el-input v-model="listQuery.createdName" placeholder="请输入创建人" clearable/>
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="gmtCreated"
          show-header-overflow
          show-overflow
          title="创建时间"
          min-width="420"
          sortable
          :resizable="false"
        />
        <template v-slot:header>
          <el-date-picker
            v-model="gmtCreatedRange"
            type="daterange"
            value-format="yyyy-MM-dd"
            :picker-options="pickerOptions"
            style="width:100%"
            range-separator="至"

            start-placeholder="开始日期"
            end-placeholder="结束日期"
            @change="value => changeDate(value, 'gmtCreated')"
          />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column field="modifiedName" title="修改人" min-width="150" sortable/>
        <template v-slot:header="{ column }">
          <el-input v-model="listQuery.modifiedName" placeholder="请输入修改人" clearable/>
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="gmtModified"
          show-header-overflow
          show-overflow
          title="修改时间"
          min-width="420"
          sortable
          :resizable="false"
        />
        <template v-slot:header>
          <el-date-picker
            v-model="gmtModifiedRange"
            type="daterange"
            value-format="yyyy-MM-dd"
            :picker-options="pickerOptions"
            style="width:100%"
            range-separator="至"

            start-placeholder="开始日期"
            end-placeholder="结束日期"
            @change="value => changeDate(value, 'gmtModified')"
          />
        </template>
      </vxe-table-column>

      <vxe-table-column title="操作" fixed="right" min-width="150">
        <template slot-scope="scope">
          <el-tooltip  effect="dark" content="详情" placement="top">
            <vxe-button type="text" @click="funcDetail(scope.row.code)">
              <i class="el-icon-document"/>
            </vxe-button>
          </el-tooltip>
          <el-tooltip  effect="dark" content="编辑" placement="top">
            <vxe-button type="text" @click="funcEdit(scope.row.code)">
              <i class="el-icon-edit"/>
            </vxe-button>
          </el-tooltip>
          <el-tooltip  v-if="scope.row.status != 1"  effect="dark" content="删除" placement="top">
            <vxe-button type="text" @click="funcDelete(scope.row.code)">
              <i class="el-icon-delete"/>
            </vxe-button>
          </el-tooltip>
        </template>
      </vxe-table-column>
    </vxe-table>
    <vxe-pager
      :loading="loading"
      :current-page.sync="listQuery.pageNum"
      :page-size.sync="listQuery.pageSize"
      :total="listQuery.total"
      :layouts="['PrevJump', 'PrevPage', 'Number', 'NextPage', 'NextJump', 'Sizes', 'FullJump', 'Total']"
      @page-change="handlePageChange"
    />

    <vxe-modal v-model="modelAddFlag" title="新增" width="800" height="700" destroy-on-close resize>
      <compAdd ref="addRef" @fatherAddMethod="fatherAdd" @fatherCancelMethod="fatherCancel"/>
    </vxe-modal>
    <vxe-modal v-model="modelEditFlag" title="编辑" width="800" height="700" destroy-on-close resize>
      <compEdit ref="editRef" @fatherEditMethod="fatherEdit" @fatherCancelMethod="fatherCancel" :operationCode="operationCode"/>
    </vxe-modal>
    <vxe-modal v-model="modelDetailFlag" title="详情" width="800" height="700" destroy-on-close resize>
      <compDetail ref="detailRef" :supplyDetail="supplyDetail"/>
    </vxe-modal>

  </div>
</template>
<script>
  import Vue from 'vue'
  import 'xe-utils'
  import VXETable from 'vxe-table'
  import 'vxe-table/lib/index.css'
  import XEUtils from 'xe-utils'
  import VXEUtils from 'vxe-utils'

  Vue.use(VXETable, VXEUtils, XEUtils, {mounts: ['cookie']})
  import compAdd from './components/supplierAdd.vue'
  import compEdit from './components/supplierEdit.vue'
  import compDetail from './components/supplierDetail.vue'
  import request from '@/utils/request'

  export default {
    name: 'supplierList',
    components: {
      compAdd,
      compEdit,
      compDetail
    },
    data() {
      return {
        operationCode:[], // 编辑
        supplyDetail:[],  //查看 接口所需id
        pickerOptions: {
          shortcuts: [
            {
              text: "最近一周",
              onClick(picker) {
                const end = new Date();
                const start = new Date();
                start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
                picker.$emit("pick", [start, end]);
              }
            },
            {
              text: "最近一个月",
              onClick(picker) {
                const end = new Date();
                const start = new Date();
                start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
                picker.$emit("pick", [start, end]);
              }
            },
            {
              text: "最近三个月",
              onClick(picker) {
                const end = new Date();
                const start = new Date();
                start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
                picker.$emit("pick", [start, end]);
              }
            }
          ]
        },
        message: '',
        loading: false,
        tableData: [],
        modelAddFlag: false,
        modelEditFlag: false,
        modelDetailFlag: false,
          gmtCreatedRange:"",
          gmtModifiedRange:"",
        copy: false,
        listQuery: {
          code: '',
          statusText: '',
          status: '',
          supplierCode: '',
          name: '',
          contacts: '',
          phone: '',
          createdName: '',//创建人
          gmtCreated: '',//创建时间
          modifiedName: '',//修改人
          gmtModified: '',//修改时间
          remark: '',
          pageNum: 1,
          pageSize: 10,
          sortField: '',
          sortType: '',
          total: 0
        }
      }
    },
    created() {
      this.findList()
    },
    methods: {
      findList() {
        this.loading = true
        return request({
          url: '/wms/supplier/list',
          method: 'get',
          params: this.listQuery
        }).then(response => {
          this.tableData = response.data.records
          this.listQuery.total = response.data.total
          this.loading = false
        })
      },
      changeDate(value, prefix) {
        if (value) {
          this.listQuery[`${prefix}Start`] = value[0] + " 00:00:00";
          this.listQuery[`${prefix}End`] = value[1] + " 23:59:59";
        } else {
          this.listQuery[`${prefix}Start`] = "";
          this.listQuery[`${prefix}End`] = "";
        }
      },
      // 排序
      sortList({column, property, order}) {
        if (order) {
          this.listQuery.sortField = property
          this.listQuery.sortType = order
        } else {
          this.listQuery.sortField = ''
        }
        this.findList()
      },
      // 添加方法
      funcAdd() {
        this.modelAddFlag = true
      },
      // 修改方法
      funcEdit(code) {
        this.operationCode = code
        this.modelEditFlag = true
      },
      // 详情方法
      funcDetail(code) {
        this.supplyDetail = code
        this.modelDetailFlag = true
      },
      // 删除方法
      funcDelete(row) {
        this.$confirm('确认删除吗？').then(_ => {
          request({url: `/wms/supplier/batchdel`, method: 'delete', params: {codes: row}}).then(response => {
            this.$message({message: response.msg, type: 'success'})
            this.findList()
          })
        }).catch(_ => {
        })
      },
      // 增加组件的父方法
      fatherAdd() {
        this.modelAddFlag = false
        this.findList()
      },
      // 编辑组件的父方法
      fatherEdit() {
        this.modelEditFlag = false
        this.findList()
      },
      // 公共取消
      fatherCancel(modelFlag) {
        switch (modelFlag) {
          case 'add':
            this.modelAddFlag = false
            break;
          case 'edit':
            this.modelEditFlag = false
            break;
          case 'detail':
            this.modelDetailFlag = false
            break;
          default:
        }
      },
      // 分页handle
      handlePageChange({currentPage, pageSize}) {
        this.listQuery.pageNum = currentPage
        this.listQuery.pageSize = pageSize
        this.findList()
      },
      // 底部颜色设置
      footerCellClassName({$rowIndex, column, columnIndex}) {
        if (columnIndex === 0) {
          if ($rowIndex === 0) {
            return 'col-blue'
          } else {
            return 'col-red'
          }
        }
      },
      // 设置底部合计
      footerMethod({columns, data}) {
        return [
          columns.map((column, columnIndex) => {
            if (columnIndex === 0) {
              return '合计'
            }
            if (['pageviews', 'forecast'].includes(column.property)) {
              return XEUtils.sum(data, column.property)
            }
            return null
          })
        ]
      }
    }
  }
</script>
